package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取行程学生信息
func GetTravelStudentInfo(p map[string]interface{}) *models.TravelStudent {
	o := orm.NewOrm()
	travelStudent := &models.TravelStudent{}
	qs := o.QueryTable("TravelStudent")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["travelId"]; ok {
		qs = qs.Filter("travel_id", p["travelId"])
	}
	if _, ok := p["studentId"]; ok {
		qs = qs.Filter("student_id", p["studentId"])
	}
	qs.One(travelStudent)
	return travelStudent
}

// 添加行程学生信息
func AddTravelStudent(input url.Values) (int64, error) {
	o := orm.NewOrm()
	travelStudent := &models.TravelStudent{}

	travel := &models.Travel{}
	if _, ok := input["travel_id"]; ok {
		travel.Id = utils.Atoi64(input["travel_id"][0])
		o.Read(travel)
	}
	travelStudent.Travel = travel

	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	travelStudent.Student = student

	return o.Insert(travelStudent)
}
